//
//  ARTWatchHisModel.m
//  ChartsDemo
//
//  Created by 刘权 on 2021/10/27.
//

#import "ARTWatchHisModel.h"

@implementation ARTWatchHisModel

+ (NSDictionary *)mj_objectClassInArray{
    return @{ @"dataList" : [ARTWathDataModel class]};
}

- (void)syncStoreData{
    _lookState = 1;
    if (_time_start && _time_end) {
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:_time_start];
        _date = [date j_stringWithFormat:@"yyyy/MM/dd HH:mm:ss"];
        
        _longTime = _time_end - _time_start;
    }
    if (_dataList.count > 0) {
        NSString *jsonString = [_dataList modelToJSONString];
        
        // 加密
        jsonString = [RSAEncryptor encryptSubsectionString:jsonString publicKey:ART_Pubkey];
        
        NSString *filePath = [J_Path_Document stringByAppendingPathComponent:@"watchHistory"];
        if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
            //如果不存在
            [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];//创建文件夹
        }
        NSInteger index = (arc4random()%1000);
        _dataFileName = [NSString stringWithFormat:@"%@%@.txt",[[NSDate date] j_stringWithFormat:@"yyyy-MM-dd_HH:mm:ss"],@(index)];
        NSString *watchFilePath = [filePath stringByAppendingPathComponent:_dataFileName];
        BOOL isSave = [jsonString writeToFile:watchFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        if (isSave) {
            NSLog(@"存储成功");
            [_dataList removeAllObjects];
        }
    }
}

- (void)storeSleepSpo2ChartImage:(UIImage *)image{
    NSString *filePath = [J_Path_Document stringByAppendingPathComponent:@"watchSleepChart"];
    if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        //如果不存在
        [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];//创建文件夹
    }
    NSString *watchFilePath = [filePath stringByAppendingPathComponent:_dataFileName];
    NSData *data = UIImagePNGRepresentation(image);
    [data writeToFile:watchFilePath atomically:YES];
}
- (UIImage *)sleepSpo2ChartImage{
    NSString *filePath = [J_Path_Document stringByAppendingPathComponent:@"watchSleepChart"];
    NSString *watchFilePath = [filePath stringByAppendingPathComponent:_dataFileName];
    NSData *data = [NSData dataWithContentsOfFile:watchFilePath];
    return [UIImage imageWithData:data];
}

- (void)storeSleepPrChartImage:(UIImage *)image{
    NSString *filePath = [J_Path_Document stringByAppendingPathComponent:@"watchSleepPrChart"];
    if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        //如果不存在
        [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];//创建文件夹
    }
    NSString *watchFilePath = [filePath stringByAppendingPathComponent:_dataFileName];
    NSData *data = UIImagePNGRepresentation(image);
    [data writeToFile:watchFilePath atomically:YES];
}
- (UIImage *)sleepPrChartImage{
    NSString *filePath = [J_Path_Document stringByAppendingPathComponent:@"watchSleepPrChart"];
    NSString *watchFilePath = [filePath stringByAppendingPathComponent:_dataFileName];
    NSData *data = [NSData dataWithContentsOfFile:watchFilePath];
    return [UIImage imageWithData:data];
}

- (void)save{
    [self syncStoreData];
    if (_type == ARTWatchMonitorTypeDaily) {
//        NSArray *historys = [[NSUserDefaults standardUserDefaults] objectForKey:App_History_DailyWatch];
//        NSMutableArray *tempArr = [NSMutableArray arrayWithArray:historys];
//        [tempArr insertObject:[self mj_JSONObject] atIndex:0];
//        [NSUserDefaults jk_setObject:tempArr forKey:App_History_DailyWatch];
        
        NSArray *historys = [[NSUserDefaults standardUserDefaults] objectForKey:App_History_DailyWatch];
        NSString *dict = [self mj_JSONString];
        dict = [RSAEncryptor encryptSubsectionString:dict publicKey:ART_Pubkey];
        NSMutableArray *tempArr = [NSMutableArray arrayWithArray:historys];
        [tempArr insertObject:dict atIndex:0];
        [NSUserDefaults jk_setObject:tempArr forKey:App_History_DailyWatch];
    }else if (_type == ARTWatchMonitorTypeSleep){
//        NSArray *historys = [[NSUserDefaults standardUserDefaults] objectForKey:App_History_SleepWatch];
//        NSMutableArray *tempArr = [NSMutableArray arrayWithArray:historys];
//        [tempArr insertObject:[self mj_JSONObject] atIndex:0];
//        [NSUserDefaults jk_setObject:tempArr forKey:App_History_SleepWatch];
        
        NSArray *historys = [[NSUserDefaults standardUserDefaults] objectForKey:App_History_SleepWatch];
        NSString *dict = [self mj_JSONString];
        dict = [RSAEncryptor encryptSubsectionString:dict publicKey:ART_Pubkey];
        NSMutableArray *tempArr = [NSMutableArray arrayWithArray:historys];
        [tempArr insertObject:dict atIndex:0];
        [NSUserDefaults jk_setObject:tempArr forKey:App_History_SleepWatch];
    }
}

@end
